#include "stdafx.h"
#include "progress_dialog_example.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QProgressBar>
#include <QProgressDialog>
#include <QPushButton>
#include <QLabel>
#include <QTimer>
#include <QMessageBox>
#include <QFont>

ProgressDialogExample::ProgressDialogExample(QWidget* parent)
    : QWidget(parent), currentProgress(0)
{
    setupUI();
    setupConnections();
}

void ProgressDialogExample::setupUI()
{
    setWindowTitle("Qt 进度对话框示例 - QProgressDialog");
    setFixedSize(400, 250);

    QVBoxLayout* mainLayout = new QVBoxLayout(this);

    // 标题
    QLabel* titleLabel = new QLabel("进度对话框演示");
    titleLabel->setAlignment(Qt::AlignCenter);
    QFont titleFont = titleLabel->font();
    titleFont.setPointSize(14);
    titleFont.setBold(true);
    titleLabel->setFont(titleFont);

    // 嵌入式进度条
    QLabel* embeddedLabel = new QLabel("总体进度:");
    embeddedProgress = new QProgressBar();
    embeddedProgress->setRange(0, 100);
    embeddedProgress->setValue(0);
    embeddedProgress->setFormat("总体进度: %p%");

    // 状态标签
    statusLabel = new QLabel("点击按钮开始长期任务");
    statusLabel->setAlignment(Qt::AlignCenter);

    // 按钮
    QPushButton* longTaskButton = new QPushButton("开始长期任务");
    QPushButton* infoButton = new QPushButton("使用说明");

    QHBoxLayout* buttonLayout = new QHBoxLayout();
    buttonLayout->addWidget(longTaskButton);
    buttonLayout->addWidget(infoButton);

    // 添加到布局
    mainLayout->addWidget(titleLabel);
    mainLayout->addSpacing(10);
    mainLayout->addWidget(embeddedLabel);
    mainLayout->addWidget(embeddedProgress);
    mainLayout->addWidget(statusLabel);
    mainLayout->addSpacing(20);
    mainLayout->addLayout(buttonLayout);
}

void ProgressDialogExample::setupConnections()
{
    // 查找按钮并连接信号
    QPushButton* longTaskButton = findChild<QPushButton*>();
    QPushButton* infoButton = findChild<QPushButton*>();

    if (longTaskButton) {
        connect(longTaskButton, &QPushButton::clicked, this, &ProgressDialogExample::startLongTask);
    }
    if (infoButton) {
        connect(infoButton, &QPushButton::clicked, this, &ProgressDialogExample::showInfo);
    }
}

void ProgressDialogExample::startLongTask()
{
    currentProgress = 0;
    embeddedProgress->setValue(0);

    QProgressDialog* progressDialog = new QProgressDialog("正在处理重要任务，请稍候...", "取消", 0, 100, this);
    progressDialog->setWindowTitle("任务进度");
    progressDialog->setWindowModality(Qt::WindowModal);
    progressDialog->setAutoClose(true);
    progressDialog->setAutoReset(true);
    progressDialog->setMinimumDuration(0);

    progressDialog->show();

    QTimer* taskTimer = new QTimer(this);

    connect(taskTimer, &QTimer::timeout, [=]() mutable {
        currentProgress += 1;

        embeddedProgress->setValue(currentProgress);
        progressDialog->setValue(currentProgress);
        statusLabel->setText(QString("处理中: %1%").arg(currentProgress));

        if (currentProgress >= 100) {
            taskTimer->stop();
            progressDialog->close();
            statusLabel->setText("长期任务完成!");
            QMessageBox::information(this, "完成", "长期任务执行成功!");
            progressDialog->deleteLater();
            taskTimer->deleteLater();
        }
        });

    connect(progressDialog, &QProgressDialog::canceled, [=]() {
        taskTimer->stop();
        statusLabel->setText("任务已取消");
        QMessageBox::information(this, "取消", "任务已被用户取消");
        progressDialog->deleteLater();
        taskTimer->deleteLater();
        });

    taskTimer->start(50);
}

void ProgressDialogExample::showInfo()
{
    QMessageBox::information(this, "使用说明",
        "这个示例演示了 QProgressDialog 的使用：\n\n"
        "• 点击按钮开始模拟长时间任务\n"
        "• 进度对话框会模态显示进度\n"
        "• 可以点击取消按钮中断任务\n"
        "• 嵌入式进度条显示总体进度");
}